package 算法学习计划;

public class 有序数组的平方 {

    public static  int[] sortedSquares(int[] nums){
        int[] newArray = new int[nums.length];

        int right = nums.length - 1;
        int left = 0;
        int i = nums.length - 1;
        int  doubleNumsLeft =  nums[0] * nums[0];
        int  doubleNumsRight =  nums[nums.length - 1] * nums[nums.length - 1];
        while (right >= left){
            if (doubleNumsLeft >= doubleNumsRight){
                newArray[i--] = doubleNumsLeft;
                left++;
                if (left <= right){
                    doubleNumsLeft = nums[left] * nums[left];
                }
            }else {
                newArray[i--] = doubleNumsRight;
                right--;
                if (right > 0){
                    doubleNumsRight = nums[right] * nums[right];
                }
            }
        }
        return newArray;
    }

    public static void main(String[] args) {
        int[]  nums = {0,2};
        System.out.println(sortedSquares(nums));
    }
}
